package org.flowers.unbeaten.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.flowers.unbeaten.base.constant.Constants;
import org.flowers.unbeaten.base.entity.Log;
import org.flowers.unbeaten.base.entity.User;
import org.flowers.unbeaten.logging.aop.enums.RequestWay;
import org.flowers.unbeaten.service.*;
import org.flowers.unbeaten.base.service.IBaseService;
import org.flowers.unbeaten.util.HttpSessionUtils;
import org.flowers.unbeaten.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @description 对外开放的公用服务类实现类
 * @author RyanWang
 * @date 2021-08-02 17:54:37
 * @version 1.0
 */
@Service
public class BaseServiceImpl implements IBaseService {

    @Autowired
    private ILogService logService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private HttpSessionUtils httpSessionUtils;


    @Override
    public Boolean save(Log log) {
        log.setOperateAddress(ServletUtils.getRemoteHost());
        log.setRequestWay(RequestWay.valueOf(ServletUtils.getMethod()));
        log.setRequestMethod(ServletUtils.getRequestURI());
        log.setBrowser(ServletUtils.getBrowser());
        log.setOs(ServletUtils.getSystem());
        String operateUrl = log.getOperateUrl();
        // 判断当前日志类型是否是登录
        if (operateUrl.contains(Constants.LOGIN_INTERFACE_NAME)) {
            String param = log.getRequestParam();
            String prefix = param.replace("[", "");
            String suffix = prefix.replace("]", "");
            JSONObject s = JSON.parseObject(suffix);
            String operator = s.getString("username");
            log.setOperator(operator);
        } else {
            String currentUserId = httpSessionUtils.getCurrentUserId();
            if (StringUtils.isNotBlank(currentUserId)) {
                User user = userService.getById(currentUserId);
                log.setOperator(user.getUsername());
            } else {
                log.setOperator("未登录用户");
            }
        }
        return logService.save(log);
    }

    @Override
    public List<String> selectUserIdsByRoleId(String roleId) {
        List<String> list = userRoleService.getUserIdsByRoleId(roleId);
        return list;
    }


    @Override
    public List<String> selectUserIdsByMenuId(String menuId) {
        List<String> list = menuService.getUserIdsByMenuId(menuId);
        return list;
    }

    @Override
    public List<String> selectRoleNames(String userId) {
        List<String> list = roleService.getRoleNames(userId);
        return list;
    }

    @Override
    public Set<String> selectPermissionsByUserId(String userId) {
        Set<String> set = menuService.getPermissionsByUserId(userId);
        return set;
    }
}
